﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

using Framework.Graphics;
using Framework.Map;
using Framework.GameStateManagment;
using Framework.GameObjectManagment;
using Framework.GameStateManagment.GameScreens;

namespace Framework.Graphics.Light
{
    public static class LightManager
    {
        static List<Light> _lights;
        private static QuadRenderComponent _render;
        private static ShadowmapResolver _shadowmapResolver;
        private static GraphicsDevice _graphicsDevice;
        private static RenderTarget2D _screenShadows;
        private static Texture2D _black;

        public static List<Light> LightsList
        {
            get { return _lights; }
            set { _lights = value; }
        }

        public static GraphicsDevice GraphicsDevice
        {
            get { return _graphicsDevice; }
            set { _graphicsDevice = value; }
        }

        public static QuadRenderComponent QRC
        {
            get { return _render; }
            set { _render = value; }
        }

        public static void Init(GraphicsDevice GD, Game game)
        {
            _black = game.Content.Load<Texture2D>("Black");
            _graphicsDevice = GD;
            _render = new QuadRenderComponent(game);
            _shadowmapResolver = new ShadowmapResolver(GD, _render, ShadowmapSize.Size512, ShadowmapSize.Size512);
            _shadowmapResolver.LoadContent(game.Content);
            _screenShadows = new RenderTarget2D(GD, GD.Viewport.Width, GD.Viewport.Height * 2);
            _render.LoadContent();
        }

        public static void Draw(TileMap map,GameObjectManager gom)
        {
            for (int i = 0; i < _lights.Count; i++)
            {
                //_lights[i].Position += Input.MovementNow();
                _lights[i].LightArea.LightPosition = _lights[i].Position + Input.Movement;
                _lights[i].LightArea.BeginDrawingShadowCasters();
                DrawCasters(_lights[i].LightArea, map,gom);
                _lights[i].LightArea.EndDrawingShadowCasters();
                _shadowmapResolver.ResolveShadows(_lights[i].LightArea.RenderTarget, _lights[i].LightArea.RenderTarget, _lights[i].Position);
            }

            _graphicsDevice.SetRenderTarget(_screenShadows);
            _graphicsDevice.Clear(Color.Black);
            Camera.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
            for (int i = 0; i < _lights.Count; i++)
            {
                Camera.TryDraw(_lights[i].LightArea.RenderTarget, _lights[i].Position+Input.Movement - _lights[i].LightArea.LightAreaSize * 0.5f, _lights[i].Color);
            }
            Camera.SpriteBatch.End();

            _graphicsDevice.SetRenderTarget(null);
            _graphicsDevice.Clear(Color.Black);

            Camera.SpriteBatch.Begin();
            map.DrawGround();
            Camera.SpriteBatch.End();

            BlendState blendState = new BlendState();
            blendState.ColorSourceBlend = Blend.DestinationColor;
            blendState.ColorDestinationBlend = Blend.SourceColor;

            Camera.SpriteBatch.Begin(SpriteSortMode.Immediate, blendState);
            Camera.TryDraw(_screenShadows, new Vector2(0, 0), Color.White);
            Camera.SpriteBatch.End();

            Camera.SpriteBatch.Begin();
            map.DrawScene();
            Camera.SpriteBatch.End();
            
        }

        private static void DrawCasters(LightArea lightArea, TileMap map, GameObjectManager gom)
        {

            Camera.SpriteBatch.Begin();
            //Hindernisse für das Licht zeichnen
            for (int y = 0; y < map.MapHeight; y++)
            {
                for (int x = 0; x < map.MapWidth; x++)
                {
                    Vector2 Temp = lightArea.ToRelativePosition(new Vector2(map.AllTiles[x, y].TileX * 50, map.AllTiles[x, y].TileY * 50));
                    if (map.AllTiles[x,y].TileEnterable == false)
                    {
                        Camera.TryDrawCasters(_black, Temp, Color.Black);
                    }
                }
            }
            //GameObjektManager.DrawShadows(lightArea);
            //Camera.SpriteBatch.Draw(Player, lightArea.ToRelativePosition(new Vector2(512, 362)), null, Color.White, 0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0f);

            Camera.SpriteBatch.End();         
            
            gom.DrawShadowObjects(lightArea);
        }
    }
}
